package org.example.commerce.imp.controller;


import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.example.commerce.api.beans.OrderInfo;
import org.example.commerce.api.beans.OrderParam;
import org.example.commerce.api.beans.SelectOrderParam;
import org.example.commerce.dao.entity.Order;
import org.example.commerce.dao.vo.OrderVo;
import org.example.commerce.imp.service.OrderService;
import org.example.common.response.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@Slf4j
@CrossOrigin
@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    OrderService orderService;

    /**
     *     系统生成流水号：code
     */
    @GetMapping("/code")
    public String GenerateCode(){
        return orderService.generatecode();
    }

    /**
     * 下订单
     */
    @PostMapping("/")
    public R<OrderInfo> addOrder(@Valid @RequestBody OrderParam request){
        log.info("Create order: data={}", request);
        return orderService.createOrder(request);
    }

    /**
     * 订单详情条件查询
     */
    @PutMapping("/")
    public R<List<OrderVo>> selectOrder(@Valid @RequestBody SelectOrderParam request){
        log.info("Create order by param {}");
        return orderService.selectOrder(request);
    }

    /**
     * 分页查询订单
     */
    @GetMapping("/{page}/{pageSize}")
    public PageInfo<Order> selectAllOrder(@PathVariable Integer page, @PathVariable Integer pageSize){
        log.info("select all orders");
        return orderService.selectAllOrder(page,pageSize);
    }

    /**
     * 订单删除
     */
    @DeleteMapping("/{code}")
    public R<String> deleteOrderByCode(@PathVariable String code){
        log.info("delect order by code = {}",code);
        return orderService.deleteOrderByCode(code);
    }
}
